<?php

/**
 * This is the model class for table "profile".
 *
 * The followings are the available columns in table 'profile':
 * @property integer $user_id
 * @property integer $gender
 * @property string $birthday
 * @property string $firstname
 * @property string $lastname
 * @property string $identify_card_number
 * @property string $identify_card_picture
 * @property string $mobile
 * @property string $tel
 * @property string $company
 * @property string $picture
 * @property string $lang
 * @property string $aboutme
 */
class Profile extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Profile the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'profiles';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id', 'required'),
            array('firstname, lastname', 'required', 'on'=>'update'),
			array('user_id, gender, birthday, identify_card_number', 'numerical', 'integerOnly'=>true),
			array('firstname, lastname, company', 'length', 'max'=>100),
			array('picture, identify_card_picture', 'length', 'max'=>50),
            array('picture, identify_card_picture', 'file', 'types'=>'jpeg,jpg,gif,png'),
			array('lang', 'length', 'max'=>3),
            array('gender', 'in', 'range'=>array(0,1)),
			array('aboutme', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, gender, birthday, firstname, lastname, identify_card_number, picture, lang, aboutme, company, mobile, tel', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user'=>array(self::HAS_ONE, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => Yii::t('label','Người dùng'),
			'gender' => Yii::t('label','Giới tính'),
            'birthday' => Yii::t('label','Ngày sinh'),
			'firstname' => Yii::t('label','Họ và tên đệm'),
			'lastname' => Yii::t('label','Tên'),
            'identify_card_number' => Yii::t('label','Số CMND'),
            'identify_card_picture' => Yii::t('label','Hình CMND'),
			'mobile'=>Yii::t('label','Số ĐT di động'),
			'tel'=>Yii::t('label','Số ĐT cố định'),
			'company'=>Yii::t('label','Tên Công ty'),
			'picture' => Yii::t('label','Ảnh đại diện'),
			'lang' => Yii::t('label','Ngôn ngữ'),
			'aboutme' => Yii::t('label','Giới thiệu bản thân'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('gender',$this->gender);
        $criteria->compare('birthday',$this->birthday);
		$criteria->compare('firstname',$this->firstname,true);
		$criteria->compare('lastname',$this->lastname,true);
		$criteria->compare('tel',$this->tel);
		$criteria->compare('mobile',$this->mobile);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('picture',$this->picture,true);
		$criteria->compare('lang',$this->lang,true);
		$criteria->compare('aboutme',$this->aboutme,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}